package com.example.blackbox.Broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.blackbox.Adapter.OtherNewsAdapter;
import com.example.blackbox.Data.MineInfo;
import com.example.blackbox.Data.News;
import com.example.blackbox.DataBase.OpDataBase;
import com.example.blackbox.R;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class MineBroadcast extends BroadcastReceiver {

    ImageView head;
    TextView name,autograph;
    Activity activity;

    public MineBroadcast(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        if("MINE_INFO_CHANGES_ACTION".equals(action)){
            head=activity.findViewById(R.id.mine_iv_head);
            name=activity.findViewById(R.id.mine_tv_name);
            autograph=activity.findViewById(R.id.mine_tv_autograph);

            MineInfo mineInfo=(MineInfo)intent.getSerializableExtra("mineinfo");

            Bitmap bitmap= BitmapFactory.decodeByteArray(mineInfo.getBytes(),0,mineInfo.getBytes().length);
            name.setText(mineInfo.getName());
            autograph.setText(mineInfo.getAutograph());
            head.setImageBitmap(bitmap);

        }else if("SIGN_INFO_ACTION".equals(action)){
            TextView username=activity.findViewById(R.id.login_et_username);
            TextView password=activity.findViewById(R.id.login_et_password);

            username.setText(intent.getStringExtra("username"));
            password.setText(intent.getStringExtra("password"));

        }else if ("EXIT_INFO_ACTION".equals(action)){
            activity.finish();
        } else {

        }
        if ("DELETE_NEWS_ACTION".equals(action)){
            TextView collection=activity.findViewById(R.id.mine_tv_collections);
            SharedPreferences sp=activity.getSharedPreferences("userinfo",Context.MODE_PRIVATE);
            String num=sp.getString("collections","0");
            collection.setText(num);

        }
        if ("DELETE_NEWS_DATA_ACTION".equals(action)){
            ListView ls=activity.findViewById(R.id.collection_ls);
            OpDataBase dataBase=new OpDataBase(activity);
            ArrayList<News> allData = dataBase.findAllData();
            OtherNewsAdapter adapter=new OtherNewsAdapter(allData);
            ls.setAdapter(adapter);
        }
    }
}
